package com.lanlian.cayman.view;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.ChooseBankAdapter;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BankInfo;
import com.lanlian.utillib.DisplayUtil;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/22 20:09
 *     desc   : 提现选择银行
 *     version: 1.0
 * </pre>
 */
public class ChooseBankDialog extends Dialog {

    private BaseActivity mContext;
    private TextView mTvCancel;
    private RecyclerView mRecycleView;

    private ArrayList<BankInfo> mList;
    private OnChooseBankListener mOnChooseBankListener;

    public interface OnChooseBankListener {
        void onChooseBank(BankInfo bankInfo);
    }

    public ChooseBankDialog(BaseActivity context, ArrayList<BankInfo> list, OnChooseBankListener onChooseBankListener) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mList = list;
        this.mOnChooseBankListener = onChooseBankListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_bank);
        initView();
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;

        ChooseBankAdapter adapter = new ChooseBankAdapter(mContext, R.layout.adapter_choose_bank, mList);
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter.bindToRecyclerView(mRecycleView);
        mRecycleView.setAdapter(adapter);
        adapter.setChecked(mList.get(0).getId());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                adapter.setChecked(mList.get(position).getId());
                mOnChooseBankListener.onChooseBank(mList.get(position));
                dismiss();
            }
        });
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        resetRecycleViewHeight(mRecycleView);
    }

    private void initView() {
        mTvCancel = findViewById(R.id.tv_cancel);
        mRecycleView = findViewById(R.id.recycle_view);
    }

    private void resetRecycleViewHeight(RecyclerView recyclerView) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter == null) {
            return;
        }

        ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
        if (mList.size() > 5) {
            params.height = DisplayUtil.dip2px(mContext, 50) * 5;
        } else {
            params.height = DisplayUtil.dip2px(mContext, 50) * mList.size();
        }
        recyclerView.setLayoutParams(params);
    }

    public void showDialog() {
        Window w = getWindow();
        w.setWindowAnimations(R.style.animFromBottom);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        show();
    }

}
